package com.ycg.biquge.aop;

import cn.hutool.core.util.ObjectUtil;
import com.ycg.biquge.common.Result;
import com.ycg.biquge.pojo.Chapter;
import com.ycg.biquge.pojo.vo.DetailNovelVo;
import jakarta.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import static com.ycg.biquge.constant.CacheConstant.*;

/**
 * @author YCG
 * @date 2024-05-12 16:03
 * @description 给小说算分的切面类
 */
@Aspect
@Component("scoreAop")
public class ScoreAOP {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Pointcut("execution(public * com.ycg.biquge.controller.ChapterController.*(..))")
    public void chapterScore(){};

    @Pointcut("execution(public * com.ycg.biquge.controller.NovelController.getDetailById(..))")
    public void novelScore(){};

    /**
     * 每进入一次章节1分
     * @param result 主要是小说id
     */
    @AfterReturning(pointcut = "chapterScore()",returning = "result")
    public void addChapterScore(Result<Chapter> result) {
        // 进入站章节页面
        Chapter chapter = result.getData();
        if (ObjectUtil.isNull(chapter)) return;
        stringRedisTemplate.opsForList().leftPush(CHAPTER_SCORE_KEY,String.valueOf(chapter.getNovelId()));
    }

    /**
     * 每进入一次书籍详情2分
     * @param result 主要是小说id
     */
    @AfterReturning(pointcut = "novelScore()",returning = "result")
    public void addNovelScore(Result<DetailNovelVo> result) {
        // 进入book页面
        DetailNovelVo data = result.getData();
        if (ObjectUtil.isNull(data)) return;
        stringRedisTemplate.opsForList().leftPush(NOVEL_SCORE_KEY,String.valueOf(data.getNovel().getId()));
    }

}
